<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
  <title>用户做题管理</title>
  <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />

  <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
  <link rel="stylesheet" href="../css/font.css">
  <link rel="stylesheet" href="../css/xadmin.css">
  <link rel="stylesheet" href="../layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../css/amazeui.min.css"/>

  <script src="../scripts/jquery.min.js"></script>
  <script src="../layui/layui.js" charset="utf-8"></script>
  <script type="text/javascript" src="../js/xadmin.js"></script>
</head>
<body>

<input type="hidden" id="flag_manager" value=<%=session.getAttribute("flag_manager")%> />


<div class="x-body">
  <div class="layui-row">
    <div class="layui-form layui-col-md12 x-so" id="searchByInfo">
      <input class="layui-input" placeholder="用户昵称" name="news_class" id="user_nickname_input" onblur="search_blur()">
      <input type="number" class="layui-input" placeholder="试题id" name="news_class" autocomplete="off" id="question_id_input" onblur="search_blur()">
      <button class="layui-btn layui-btn-warm" onclick="search_button()"><i class="layui-icon">&#xe615;</i>
      </button>
    </div>
  </div>
  <xblock>
    <button class="layui-btn layui-btn-danger" onclick="user_question_table_delete_many()"><i class="layui-icon"></i>批量删除</button>
    <a href="">
        <span class="x-right" style="line-height:40px">
        <button class="layui-btn"><i class="layui-icon">&#xe65c;</i>刷新</button>
        </span>
    </a>
  </xblock>
  <table class="layui-table" id="user_question_table_id">
  </table>
</div>


<script>
  var tableIns;
  var table;
  var layer;

  var total_pages;
  var current_page;
  var limit;
  var current_rows;

  layui.use('layer', function(){
    layer = layui.layer;
  });
  layui.use('table', function(){
    table = layui.table;

    tableIns=table.render({
      elem: '#user_question_table_id'
      ,height: 312
      ,url:"${pageContext.request.contextPath}/user_question/user_question_table_select.do" //数据接口
      ,where:{
        "user_nickname":$("#user_nickname_input").val(),
        "question_id":$("#question_id_input").val(),
      }
      ,parseData:function (res){
          return {
              "code":res.code,
              "msg":res.msg,
              "count":res.count,
              "data":res.data
          }
      }
      ,page: true //开启分页
      ,limit: 10
      ,limits: [10,15,20,25,30]
      ,cols: [[ //表头
        {field: 'checkbox',title: '选框', width:"10%", type:"checkbox", align:"center", fixed: 'left'}
        ,{field: 'user_id', title: '用户id',hide:true}
        ,{field: 'user_nickname', title: '昵称hide', hide:true}
        ,{field: 'user_nickname_link', title: '用户昵称', width:"25%", toolbar: "#user_nickname_link"}
        ,{field: 'question_id', title: '试题id,hide', hide:true}
        ,{field: 'question_id_link', title: '试题id', width:"20%", toolbar: "#question_id_link", sort: true}
        ,{field: 'flag_pass', title: '通过', width:"15%"}
        ,{field: 'flag_collect', title: '收藏', width: "15%", sort: true}
        ,{field: 'operate', title: '操作', width: "15%", align:"center", toolbar:"#endBar", fixed: 'right' }
      ]]
      ,done:function(res,curr,count){
        current_page=curr;
        total_pages=Math.ceil(count/this.limit);
        limit=this.limit;
        current_rows=count-(total_pages-1)*limit;

      }
    });

  });

  function refreshPage(){
    tableIns.reload({
      where: { //设定异步数据接口的额外参数，任意设
        "user_nickname":$("#user_nickname_input").val(),
        "question_id":$("#question_id_input").val(),
      }
      , page: {
        curr: current_page //重新从第 curr 页开始
      }
    });
  }

  function search_blur(){
    tableIns.reload({
      where: { //设定异步数据接口的额外参数，任意设
        "user_nickname":$("#user_nickname_input").val(),
        "question_id":$("#question_id_input").val(),
      }
      , page: {
        curr: 1 //重新从第 curr 页开始
      }
    });
  }
  function search_button(){
    var user_nickname=$("#user_nickname_input").val();
    var question_id=$("#question_id_input").val();
    if(question_id==""&&user_nickname==""){
      layer.msg("没有填写查询信息，采用默认查询");
    }else{
      layer.msg("查询成功");
    }
  }

  function user_question_table_delete_many(){
    var check=table.checkStatus('user_question_table_id');
    var mydata=check.data;
    var user_id_array=[];
    var question_id_array=[];
    if(mydata.length==0){
      layer.msg("您还没选中任何选项")
    }else{
      for(var i=0;i<mydata.length;i++){
        user_id_array.push(mydata[i].user_id);
        question_id_array.push(mydata[i].question_id);
      }
      // console.log(news_id_array);
      layer.confirm('确定删除？',{btn:['确定','取消']},
              function(){
                if($("#flag_manager").val()!="null") {
                  $.ajax({
                    url:"${pageContext.request.contextPath}/user_question/user_question_table_delete_many.do",
                    dataType:"json",
                    data:{
                      "user_id_array":JSON.stringify(user_id_array),
                      "question_id_array":JSON.stringify(question_id_array)
                    },
                    success:function(data,status){
                      if(status=="success") {
                        layer.msg("选中项已被删除", {icon: 1});
                        refreshPage();
                      }
                    }
                  });
                }else{
                  layer.msg("删除失败，您不是管理员");
                }
              },function(){
                layer.msg("取消删除");
              }
      );
    }


  }
  function comment_table_edit(url,id_type,id){
    var w=$(window).width()*0.9;
    var h=$(window).height() - 50;
    layer.open({
      type: 2,
      area: [w+'px', h +'px'],
      fix: false, //不固定
      maxmin: true,
      shadeClose: true,
      shade:0.4,
      title: "详细信息",
      content: url+"?"+id_type+"="+id,
    });
  }
  function user_question_table_delete_one(user_id,question_id){
    layer.confirm('确定删除？',{btn:['确定','取消']},
            function(){
              if($("#flag_manager").val()!="null"){
                $.ajax({
                  url:"${pageContext.request.contextPath}/user_question/user_question_table_delete_one.do",
                  data:{
                    "user_id":user_id,
                    "question_id":question_id
                  },
                  success:function(data,status){
                    if(status=="success"){
                      layer.msg("删除成功",{icon:1});
                      refreshPage();
                    }
                  }
                });
              }else{
                layer.msg("删除失败，您不是管理员");
              }
            },function(){
              layer.msg("取消删除");
            }
    );

  }
</script>
<script type="text/html" id="user_nickname_link">
  <a href="javascript:comment_table_edit('comment_table_user_info.jsp','user_id',{{d.user_id}});" class="layui-table-link">{{d.user_nickname.substring(0,7)}}...</a>
</script>
<script type="text/html" id="question_id_link">
  <a href="javascript:comment_table_edit('comment_table_question_info.jsp','question_id',{{d.question_id}});" class="layui-table-link">{{d.question_id}}</a>
</script>
<script type="text/html" id="endBar">
  <a href="javascript:user_question_table_delete_one({{d.user_id}},{{d.question_id}});" class="layui-table-link"><i class="layui-icon">&#xe640;</i></a>
</script>
</body>
</html>
